-- AvalonLab
-- Created by dengc Sep/29/2016
-- 阿瓦隆要塞实验室

-- 初始化
function initAvalonLab(self)
    local class = self.gridData.class;
    local pos = self.gridData:getPos();
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    local elementId = self.gridData.element;
    local args = DungeonM.queryElement(elementId).args;
    local labType = args.lab_type;
    if not labType then
        trace("submit_to_lab", "获取不到实验室的类型配置：labType。");
        return;
    end

    if not buildingInfo then
        error("获取不到建筑查看配置，元素类型：" .. class);
        return;
    end

    local iconPath = getLevelItemIconPath(buildingInfo["icon"]);

    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                return;
            end

            local function okFunc()
                -- 添加地牢动作
                DungeonActionM.go("submit_to_lab", pos);
            end

            local function cancelFunc()
                -- 拒绝给予提示
                alert(getLocStr("avalon_lab_refuse_" .. labType));
            end

            -- 消耗
            local cost = self.gridData.lab_cost;

            -- 建筑描述
            local dialog = BuildingBonusM.getBuildingDialog(self.gridData);
            if labType == "gray_dwarf" then
                dialog = string.format(dialog, cost[3]);
            elseif labType == "dwarf" then
                dialog = string.format(dialog, ItemM.query(cost[2], "name"));
            else
                dialog = string.format(dialog, cost[3], FieldsM.query(cost[2], "name"));
            end

            -- 打开建筑查看界面
            AudioM.playFx("ui_open1");
            self:openBuildingView2(
                okFunc,
                cancelFunc,
                nil,
                nil,
                dialog
            );
        end
    end

    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.icon:addTouchEventListener(onClicked);
    self.item:addChild(self.icon);
    self.item:setPositionY(20);
end

-- 向实验室提交物品
function onSubmitToLab(self, args)
    local isSuccess = args["isSuccess"];
    local pos = args["pos"];
    local labType = args["labType"];

    -- 失败
    if not isSuccess then
        -- 给予提示
        if labType == "goblin" then
            local arr = string.explode(getLocStr("avalon_lab_cannot_submit_goblin"), "\n");
            splitAlert(arr);
        elseif labType == "dwarf" then
            -- 如果身上有装备，那么给予特别的提示
            local cost = self.gridData.lab_cost;
            if sizeof(cost) > 0 and EquipM.isEquiped(cost[2]) then
                alert(getLocStr("avalon_lab_cannot_submit_dwarf"));
            else
                alert(getLocStr("avalon_lab_cannot_submit"));
            end
        else
            alert(getLocStr("avalon_lab_cannot_submit"));
        end

        return;
    end

    -- 成功的奖励提示
    local bonus = args["bonus"];
    local arr = {};

    if labType == "goblin" then
        -- 地精奖励的提示
        if bonus[1] == "manual" then
            arr = string.explode(getLocStr("avalon_lab_goblin_manual"), "\n");
            arr[2] = string.format(arr[2], EngineeringManualM.query(bonus[2], "name"));
        else
            arr = string.explode(getLocStr("avalon_lab_goblin_item"), "\n");
        end

        splitAlert(arr);
    else
        -- 灰矮人和矮人的奖励提示
        if bonus[1] == 1 then
            alert(getLocStr("avalon_lab_item"));
        elseif bonus[1] == 2 then
            arr = string.explode(getLocStr("avalon_lab_attrib"), "\n");
            splitAlert(arr);
        else
            arr = string.explode(getLocStr("avalon_lab_manual"), "\n");
            arr[2] = string.format(arr[2], EngineeringManualM.query(bonus[2], "name"));
            splitAlert(arr);
        end
    end

    -- 建筑物奖励事件
    if bonus[1] ~= "manual" then
        self:onBuildingBonus({["bonus"] = bonus});
    else
        playBuildingDisappear(self);
    end
end
